package park.another_prospect;

import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class ParkingLane
{
    private static Timer parkTimer = new Timer(true);
    private final int id;

    private List<CarObserver> cars = new LinkedList<CarObserver>();

    public ParkingLane(final ParkingLot parkingLot, int id)
    {
        this.id = id;
        // 10s per car
        parkTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                if (parkingLot.isAvailable() && cars.size() > 0)
                {
                    CarObserver observer;
                    (observer = cars.remove(0)).park(parkingLot);
                    observer.update(null, null);
                }
            }
        }, 10, 10000);
    }

    public void add(CarObserver car)
    {
        cars.add(car);
    }

    @Override
    public String toString()
    {
        return "ParkingLane_" + id + "{" +
                "waiting in list =" + cars.size() +
                '}';
    }
}
